﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ManWebRole.Models;

namespace ManWebRole.Controllers.Administrator
{
    public class AdministratorController : Controller
    {
        //
        // GET: /Administrator/

        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult AccountDetails(Account account)
        {
            AccountModel model = new AccountModel();
            Account latestAccount = model.GetAccountById(account.Id);
            return View(latestAccount);
        }

        //
        // POST: /Administrator/Create

        [HttpPost]
        public ActionResult AccountCreate(Account account)
        {
            if (!ModelState.IsValid)
            {
                return View(account);
            }
            AccountModel model = new AccountModel();
            model.AddAccount(account);
            return RedirectToAction("AccountList");
        }

        //
        // POST: /Administrator/Create

        public ActionResult AccountCreate()
        {
            return View();
        }

        //
        // GET: /Administrator/Edit/5

        public ActionResult AccountEdit(int id)
        {
            AccountModel model = new AccountModel();
            Account account = model.GetAccountById(id);
            return View(account);
        }

        //
        // POST: /Administrator/Edit/5

        [HttpPost]
        public ActionResult AccountEdit(Account account)
        {
            if (!ModelState.IsValid)
            {
                return View("AccountEdit", account);
            }
            AccountModel model = new AccountModel();
            account.ProjectId = Int32.Parse(Request.Form.Get("projectSelect"));
            account.Type = (byte)Int32.Parse(Request.Form.Get("userTypeSelect"));
            model.UpdateAccount(account);
            return RedirectToAction("AccountList");
        }

        //
        // GET: /Administrator/Delete/5

        public ActionResult AccountDelete(int id)
        {
            AccountModel model = new AccountModel();
            Account account = model.GetAccountById(id);
            return View(account);
        }

        //
        // POST: /Administrator/Delete/5

        [HttpPost]
        public ActionResult AccountDelete(Account account)
        {
            AccountModel model = new AccountModel();
            model.DeleteAccount(account.Id);
            return RedirectToAction("AccountList");
        }

        public ActionResult AccountList()
        {
            AccountModel model = new AccountModel();
            return View(model.GetAllAccounts());
        }


        public ActionResult ProjectList()
        {
            ProjectModel model = new ProjectModel();
            return View(model.GetAllProjects());
        }

        public ActionResult ProjectDetails(Project project)
        {
            ProjectModel model = new ProjectModel();
            Project latestProject = model.GetProjectById(project.Id);
            return View(latestProject);
        }

        //
        // POST: /Administrator/Create
        [HttpPost]
        public ActionResult ProjectCreate(Project project)
        {
            if (!ModelState.IsValid)
            {
                return View(project);
            }
            ProjectModel model = new ProjectModel();
            model.AddProject(project);
            SpecificationModel specModel = new SpecificationModel();
            specModel.AddNewSpecificationOnProjectCreated(project);
            return RedirectToAction("ProjectList");
        }

        //
        // POST: /Administrator/Create

        public ActionResult ProjectCreate()
        {
           return View(); 
        }

        
        public ActionResult ProjectEdit(int id)
        {
            ProjectModel model = new ProjectModel();
            Project project = model.GetProjectById(id);
            return View(project);
        }

        //
        // POST: /Administrator/Edit/5

        [HttpPost]
        public ActionResult ProjectEdit(Project project)
        {
            if (!ModelState.IsValid)
            {
                return View("ProjectEdit", project);
            }
            ProjectModel model = new ProjectModel();
            model.UpdateProject(project);
            return RedirectToAction("ProjectList");
        }

        //
        // GET: /Administrator/Delete/5

        public ActionResult ProjectDelete(int id)
        {
            ProjectModel model = new ProjectModel();
            Project project = model.GetProjectById(id);
            return View(project);
        }

        [HttpPost]
        public ActionResult ProjectDelete(Project project)
        {
            ProjectModel model = new ProjectModel();
            model.DeleteProjectById(project.Id);
            return RedirectToAction("ProjectList");
        }
    }
}
